import { Toast } from 'vant'
import {interfaceUtils} from './interface.js'
let crypto = require('crypto');
// https://www.npmjs.com/package/jsencrypt?activeTab=versions
import JsEncrypt from "jsencrypt";

class Area extends interfaceUtils {
    static IsRequire=1
    static Name="area"
    static TimeStamp=new Date().getTime()
    constructor(phone){
        super()   
        this.phone=phone==""?"null":phone 
        this.timeStamp=new Date().getTime()
    }
    _getParams(){     
        return 'null'
     }
     _getSign(){
        const key =Area.md5Key+""
        const {phone} =this 
        const consumerFrom=Area.consumerFrom+""
        const {timeStamp}=this
     //    https://blog.csdn.net/wail521/article/details/96430873       
        //  const sign  = this._parmaSort({ key ,phone,consumerFrom ,timeStamp  }) 
        const sign  = phone +consumerFrom+timeStamp+key
         const result = crypto.createHash('md5').update(sign).digest("hex")    
         return result 
     }
 
     _getData(){
       
         const params=this._getParams()
         const sign=this._getSign()
         const {phone} = this
         const consumerFrom=Area.consumerFrom
         const {timeStamp}=this
 
         let formData = new FormData();
         formData.append("params",params);
         formData.append("sign",sign);
         formData.append("phone",phone);
         formData.append("consumerFrom",consumerFrom);
         formData.append("timeStamp",timeStamp);
 
 
 
         return  formData
 
 
     }
 
       /**
      * 
      * @param {keyObj} 将对象排序 变成 字符串
      *  例如 a=1&b=2 
      */
     _parmaSort(keyObj){
         let keyArr = [];
       for (let key in keyObj) {
         keyArr.push(key);
       }
         let reaKryArr = keyArr.sort();
         let newArrStr = "";
         for (let i = 0; i < keyArr.length; i++) {
           newArrStr += reaKryArr[i] + "=" + keyObj[reaKryArr[i]] + "&";
         }
     
         let str = newArrStr.substr(0, newArrStr.length - 1);
         return str
     }
}

export function qualified(data){
    if(data == null || data ==""||data == undefined ){
        return false
    }else{
        return true
    }
}

export function validata(data,class_type){
    let qualify =false

    let set= Object.entries(data).slice(0,class_type.IsRequire)
    let ret= set.find((value,index)=>{
        return !qualified( value[1])
    })
    if( ret == undefined){  
        //符合要求   
        if( qualified(data.phone)   ){  
            // switch (class_type.Name) {
            //     case 'Area':
            //         if((data.AreaPassward+"").length<6 || (data.AreaPassward+"").length>20){
            //             Toast.fail({
            //                 message:"请输入6-20位密码"
            //             })
            //             qualify=false
            //         }else{
            //             qualify=true
            //         }       
            //      break;               
            // }        
            qualify=true
              return qualify
            }else{             
                return qualify
            }     
    }else{
       Toast.fail(ret[0] +" is Null");
        return false   
    }   
 


    
}

export function getProvince(list){
     /**
      * 1.根据areaDeep 值 分类 
      *    1  省
      *    2  市
      *    3  区
      */
    let provinceArr= list.filter((item)=>{
        return item.areaDeep == 1
    })
    provinceArr.forEach((item)=>{
        item.defaultV=false
    })
    // console.log(provinceArr)
    return provinceArr

}
export function getCity(list){
    /**
     * 1.根据areaDeep 值 分类 
     *    1  省
     *    2  市
     *    3  区
     */
   let cityArr= list.filter((item)=>{
       return item.areaDeep == 2
   })
   cityArr.forEach((item)=>{
       item.defaultV=false
   })
//    console.log("city",cityArr)
 
   return cityArr

}
export function getAreas(list){
    /**
     * 1.根据areaDeep 值 分类 
     *    1  省
     *    2  市
     *    3  区
     */
   let areaArr= list.filter((item)=>{
       return item.areaDeep == 3
   })
   areaArr.forEach((item)=>{
    item.defaultV=false
   })
//    console.log(areaArr)
   return areaArr

}


export { Area }